package org.xx.armory.swing.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xx.armory.swing.FrameUIController;
import org.xx.armory.swing.builders.MenuBarModel;
import org.xx.armory.swing.builders.MenuModel;
import org.xx.armory.swing.builders.UIModel;

import javax.swing.*;
import java.awt.*;

import static org.xx.armory.commons.Validators.notNull;

public class FrameUIModelController
        extends UIModelController<JFrame>
        implements FrameUIController {
    private static final int DEFAULT_FRAME_WIDTH = 1024;
    private static final int DEFAULT_FRAME_HEIGHT = 768;

    private final Logger logger = LoggerFactory.getLogger(FrameUIModelController.class);

    public FrameUIModelController(
            JFrame frame
    ) {
        super(frame);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void loadFromUIModel(
            UIModel uiModel
    ) {
        super.loadFromUIModel(uiModel);

        createTitleBar(uiModel);
        setSize(uiModel);
        createMenuBars(uiModel);

        // 创建StatusBar。

        // 创建客户区布局。
    }

    protected void createTitleBar(
            UIModel uiModel
    ) {
        final String title = uiModel.getTitle();
        if (!title.isEmpty()) {
            getContainer().setTitle(title);
        }

        final Image icon = loadImage(uiModel.getResourceName(), uiModel.getIcon());
        if (icon != null) {
            getContainer().setIconImage(icon);
        }
    }

    protected void setSize(
            UIModel uiModel
    ) {
        // 设置Frame的尺寸。
        int width = uiModel.getWidth();
        int height = uiModel.getHeight();
        if (width <= 0) {
            width = DEFAULT_FRAME_WIDTH;
        }
        if (height <= 0) {
            height = DEFAULT_FRAME_HEIGHT;
        }
        getContainer().setSize(width, height);
    }

    protected void createMenuBars(
            UIModel uiModel
    ) {
        if (uiModel.getMenuBars().isEmpty()) {
            return;
        }

        final JMenuBar menuBar = new JMenuBar();
        final MenuBarModel menuBarModel = uiModel.getMenuBars().get(0);
        for (final MenuModel menu : menuBarModel.getChildren()) {
            if ("-".equals(menu.getTitle()) && menu.getChildren().isEmpty()) {
                logger.trace("menu: -");
                menuBar.add(Box.createHorizontalStrut(12));
            } else if ("->".equals(menu.getTitle()) && menu.getChildren().isEmpty()) {
                logger.trace("menu: <--->");
                menuBar.add(Box.createHorizontalGlue());
            } else {
                logger.trace("menu: \"{}\"", menu.getTitle());
                menuBar.add(createMenu(menu));
            }
        }

        this.getContainer().setJMenuBar(menuBar);
    }

    private JMenu createMenu(
            MenuModel menu
    ) {
        notNull(menu, "menu");

        final JMenu result = new JMenu();

        result.setText(menu.getTitle());

        for (final Object item : menu.getChildren()) {
            if (item instanceof String) {
                if (((String) item).isEmpty()) {
                    logger.trace("skip empty action");
                } else if ("-".equals(item)) {
                    // 分隔符
                    result.addSeparator();
                } else {
                    // Action
                    final Action action = this.getAction((String) item);
                    if (action == null) {
                        logger.warn("cannot find action: {}", item);
                    } else {
                        result.add(action);
                    }
                }
            } else if (item instanceof MenuModel) {
                result.add(createMenu((MenuModel) item));
            }
        }

        return result;
    }
}
